package com.ruoyi.sumorder.controller;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.shop.api.RemoteShopService;
import com.ruoyi.sumorder.domain.SumOrder;
import com.ruoyi.sumorder.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.sumorder.domain.CheOrder;
import com.ruoyi.sumorder.service.ICheOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单统计Controller
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@RestController
@RequestMapping("/sumorder")
public class CheOrderController extends BaseController
{
    @Autowired
    private ICheOrderService cheOrderService;

    /**
     * 查询订单统计列表
     */
//    @RequiresPermissions("sumorder:sumorder:list")
    @GetMapping("/list")
    public TableDataInfo list(SumOrder sumOrder)
    {
        System.out.println(sumOrder.getParams());
        startPage();
        List<SumOrder> list = new ArrayList<>();
        SumOrder sum = new SumOrder();
        sum.setArea("合计");
        Long fixNum = 0L;
        Long washNum = 0L;
        Long keepNum = 0L;
        List<SumOrder> list2 = cheOrderService.selectByServiceAndArea(sumOrder);
        for (SumOrder sumOrder1 : list2) {
            fixNum = fixNum + sumOrder1.getFixNum();
            washNum = washNum + sumOrder1.getWashNum();
            keepNum = keepNum + sumOrder1.getKeepNum();
        }
        sum.setFixNum(fixNum);
        sum.setWashNum(washNum);
        sum.setKeepNum(keepNum);
        list.add(sum);
        list.addAll(1, list2);
        return getDataTable(list);
    }



    /**
     * 查询订单统计列表
     */
    @RequiresPermissions("sumorder:sumorder:list")
    @GetMapping("/list1")
    public TableDataInfo list(CheOrder cheOrder)
    {
        startPage();
        List<CheOrder> list = cheOrderService.selectCheOrderList(cheOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单统计列表
     */
    @RequiresPermissions("sumorder:sumorder:export")
    @Log(title = "订单统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheOrder cheOrder)
    {
        List<CheOrder> list = cheOrderService.selectCheOrderList(cheOrder);
        ExcelUtil<CheOrder> util = new ExcelUtil<CheOrder>(CheOrder.class);
        util.exportExcel(response, list, "订单统计数据");
    }

    /**
     * 获取订单统计详细信息
     */
    @RequiresPermissions("sumorder:sumorder:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheOrderService.selectCheOrderById(id));
    }

    /**
     * 新增订单统计
     */
    @RequiresPermissions("sumorder:sumorder:add")
    @Log(title = "订单统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheOrder cheOrder)
    {
        return toAjax(cheOrderService.insertCheOrder(cheOrder));
    }

    /**
     * 修改订单统计
     */
    @RequiresPermissions("sumorder:sumorder:edit")
    @Log(title = "订单统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheOrder cheOrder)
    {
        return toAjax(cheOrderService.updateCheOrder(cheOrder));
    }

    /**
     * 删除订单统计
     */
    @RequiresPermissions("sumorder:sumorder:remove")
    @Log(title = "订单统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheOrderService.deleteCheOrderByIds(ids));
    }
}
